<?php
/**
 * AbstractController.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/25
 * Time: 17:10
 */
namespace app\console\command\auto_curd\tpl;

class AbstractController
{
    static public function begin($dir,$baseName,$databaseName)
    {
        $className = $baseName . 'Controller';
        $date = date('Y/m/d',time());
        $time = date('H:i',time());
        $tNamespace = str_replace("/","\\",$dir).$databaseName;
        $tpl = <<<EOF
<?php
/**
 * $className.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: $date
 * Time: $time
 */
namespace app\\{$tNamespace}\\controller;

use app\\common\\auto_curd\\php_system\\model\\{$baseName}Model;
use think\\Controller;

abstract class $className extends Controller
{
EOF;
        return $tpl;
    }

    static public function create($column)
    {
        $tpl = <<<EOF

            // {$column['column_comment']}
            if(isset(\$params['{$column['column_name']}']) && \$params['{$column['column_name']}']!="")
            {
                \$addData['{$column['column_name']}'] = \$params['{$column['column_name']}'];
            }

EOF;
        return $tpl;
    }
    static public function update($column)
    {
        $tpl = <<<EOF

            // {$column['column_comment']}
            if(isset(\$params['{$column['column_name']}']) && \$params['{$column['column_name']}']!="")
            {
                \$saveData['{$column['column_name']}'] = \$params['{$column['column_name']}'];
            }

EOF;
        return $tpl;
    }
    static public function read($baseName,$tableName,$databaseName)
    {
        $tpl = <<<EOF

        \$list = {$baseName}Model::paginate([]);
        \$pagination = \$list->render();
        return view('common/auto_curd/{$databaseName}@{$tableName}/read',[
            'list' => \$list,
            'pagination' => \$pagination
        ]);

EOF;
        return $tpl;
    }
    static public function delete($baseName,$PRI_NAME)
    {
        $tpl = <<<EOF

        \$status = {$baseName}Model::delete(\$this->request->param('$PRI_NAME'));
        if(!\$status)
        {
            return \$this->error('删除失败');
        }
        else
        {
            return \$this->success('删除成功');
        }

EOF;
        return $tpl;
    }

    static public function content($create,$update,$read,$delete,$baseName,$tableName,$databaseName,$PRI_NAME)
    {
        $tpl = <<<EOF

    /**
     * 创建操作
     * @return \\think\\response\\View|void
     */
    public function create()
    {
        if(\$this->request->isPost()){
            \$params = \$this->request->post();
            \$addData = [];
$create
            \$status = {$baseName}Model::save(null,\$addData);
            if(!\$status)
            {
                return \$this->error('添加失败');
            }
            else
            {
                return \$this->success('添加成功',url('update')."?$PRI_NAME=".\$status);
            }
        }else{
            return view('common/auto_curd/{$databaseName}@{$tableName}/create');
        }
    }
    
    /**
     * 更新操作
     */
    public function update()
    {
        if(\$this->request->isPost()){
            \$params = \$this->request->post();
            \$saveData = [];
$update
            \$status = {$baseName}Model::save(\$saveData['$PRI_NAME'],\$saveData);
            if(!\$status)
            {
                return \$this->error('更新失败');
            }
            else
            {
                return \$this->success('更新成功');
            }
        }else{
            \$row = {$baseName}Model::get(['$PRI_NAME'=>\$this->request->get('$PRI_NAME')]);
            return view('common/auto_curd/{$databaseName}@{$tableName}/update',[
                'row' => \$row
            ]);
        }
    }
    /**
     * 获取操作
     */
    public function read()
    {
$read
    }
    /**
     * 删除操作
     */
    public function delete()
    {
$delete
    }
    
EOF;
        return $tpl;
    }

    static public function end()
    {
        $tpl = <<<EOF

}
EOF;
        return $tpl;
    }
}